package com.epam.web_project.bank.command.admin_commands;

import java.io.IOException;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.epam.web_project.bank.command.Command;
import com.epam.web_project.bank.dao.account_dao.AccountDAO;
import com.epam.web_project.bank.dao.customer_dao.CustomerDAO;
import com.epam.web_project.bank.dao.factory.MySQLDAOFactory;
import com.epam.web_project.bank.db.ConnectionPool;
import com.epam.web_project.bank.entity.Account;
import com.epam.web_project.bank.entity.Customer;
import com.epam.web_project.bank.util.AppPropertiesLoader;

public class MarkUnapprovedUserDeleted implements Command {

	@Override
	public String execute(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		AppPropertiesLoader props = AppPropertiesLoader.getInstance();
		String resPage = props.getProperty("show_admin_main_command");
		if (request.getParameter("custId").equals("")) {
			return resPage;
		}
		Customer customer = new Customer();
		int customerId=Integer.valueOf(request.getParameter("custId"));
		customer.setId(customerId);
		MySQLDAOFactory daoFactory = new MySQLDAOFactory();
		daoFactory.setConnectionPool(ConnectionPool.getInstance());
		CustomerDAO customerDAO=daoFactory.getCustomerDAO();
		AccountDAO accountDAO = daoFactory.getAccountDAO();
		List<Account> accounts=accountDAO.findByCustomerId(customerId, true);
		String message;
		Locale locale=request.getLocale();
		ResourceBundle bundle=ResourceBundle.getBundle("i18n", locale);
		if(customerDAO.markDeleted(customer)) {
			for(Account account:accounts) {
				accountDAO.markDeleted(account);
			}
			message=bundle.getString("java.usr_del");
		} else {
			message=bundle.getString("java.usr_del_err");
		}
		HttpSession session = request.getSession();
		session.setAttribute("message", message);
		return resPage;
	}

}
